
<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" label-width="auto" :inline="true">
        <el-form-item label="司机名称" prop="driverName">
          <el-input @keyup.enter="handleQuery" v-model="queryParams.driverName" placeholder="请输入" style="width: 256px"></el-input>
        </el-form-item>
        <el-form-item label="司机电话" prop="driverPhone">
          <el-input @keyup.enter="handleQuery" v-model="queryParams.driverPhone" placeholder="请输入" style="width: 256px"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select @keyup.enter="handleQuery" v-model="queryParams.type" placeholder="请选择" style="width: 256px">
            <el-option :value="1" label="收入"></el-option>
            <el-option :value="2" label="罚款"></el-option>
            <el-option :value="3" label="提现"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="子类型" prop="subType">
          <el-select @keyup.enter="handleQuery" v-model="queryParams.subType" placeholder="请选择" style="width: 256px">
            <el-option :value="101" label="订单收入"></el-option>
            <el-option :value="102" label="订单奖励"></el-option>
            <el-option :value="103" label="人工奖励"></el-option>
            <el-option :value="104" label="好评奖励"></el-option>
            <el-option :value="201" label="订单罚款"></el-option>
            <el-option :value="202" label="人工罚款"></el-option>
            <el-option :value="203" label="订单取消"></el-option>
            <el-option :value="301" label="提现到支付宝"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select @keyup.enter="handleQuery" v-model="queryParams.status" placeholder="请选择" style="width: 256px">
            <el-option :value="0" label="未入账"></el-option>
            <el-option :value="1" label="已入账"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="cityId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.cityId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="平台" prop="platformId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.platformId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="渠道" prop="channelId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.channelId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in channelArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="订单号" prop="orderId">
          <el-input @keyup.enter="handleQuery" placeholder="请输入" v-model="queryParams.orderId"></el-input>
        </el-form-item>
        <el-form-item label="订单时间">
          <el-date-picker
            style="width: 256px"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change="handleOrderTimeChange"
            v-model="orderTimes"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            style="width: 256px"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change="handleCreateTimeChange"
            v-model="createdTimes"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="warning" @click="handleExport">
          <template #icon><Edit /></template>
          导出
        </el-button>
      </template>
      <el-table v-loading="loading" :data="TableData" highlight-current-row row-key="id" @row-click="handleRowClick">
        <el-table-column label="姓名" min-width="120" align="center" prop="driverName"></el-table-column>
        <el-table-column label="手机" min-width="120" align="center" prop="driverPhone"></el-table-column>
        <el-table-column label="类型" min-width="80" align="center" prop="type">
          <template #default="scope">
            <div v-if="scope.row.type == 1" style="color: #298b3f">收入</div>
            <div v-else-if="scope.row.type == 2" style="color: red">罚款</div>
            <div style="color: #9806e5" v-else>提现</div>
          </template>
        </el-table-column>
        <el-table-column label="子类型" min-width="80" align="center" prop="subType">
          <template #default="scope">
            <div v-if="scope.row.subType == 101">订单收入</div>
            <div v-else-if="scope.row.subType == 102" style="color: #298b3f">订单奖励</div>
            <div v-else-if="scope.row.subType == 103" style="color: #298b3f">人工奖励</div>
            <div v-else-if="scope.row.subType == 104" style="color: #298b3f">好评奖励</div>
            <div v-else-if="scope.row.subType == 201" style="color: red">订单罚款</div>
            <div v-else-if="scope.row.subType == 202" style="color: red">人工罚款</div>
            <div v-else-if="scope.row.subType == 203" style="color: red">订单取消</div>
            <div v-else-if="scope.row.subType == 301" style="color: #9806e5">提现到支付宝</div>
            <div v-else-if="scope.row.subType == 302" style="color: #956eaa">提现手续费</div>
          </template>
        </el-table-column>
        <el-table-column label="金额" min-width="120" align="center" prop="fund"></el-table-column>
        <el-table-column label="标题" min-width="200" align="center" prop="title"></el-table-column>
        <el-table-column label="描述" min-width="120" align="center" prop="desc"></el-table-column>
        <el-table-column label="订单号" min-width="120" align="center" prop="orderId"></el-table-column>
        <el-table-column label="状态" min-width="80" align="center" prop="status">
          <template #default="scope">
            <div v-if="scope.row.status == 0">未入账</div>
            <div v-else>已入账</div>
          </template>
        </el-table-column>
        <el-table-column label="订单时间" min-width="120" align="center" prop="orderTime"></el-table-column>
        <el-table-column label="创建时间" min-width="120" align="center" prop="createTime"></el-table-column>
      </el-table>
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
    <!-- 改派 -->
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "SysMenu",
  inheritAttrs: false,
});

import CommonApi from "@/api/common";
import ExportApi from "@/api/exports";
import { orderStatusEnum, platFormEnum, channelEnum, orderTypeEnum, bizTypeEnum, luggageEnum, channelArr, bizTypeArr, orderTypeArr, platFormArr, carTypeArr } from "@/enums/common";
import { MenuTypeEnum } from "@/enums/MenuTypeEnum";
import { log } from "console";
import JntApi from "@/api/jnt";
import DriverApi from "@/api/driver";

const queryFormRef = ref(ElForm);
const total = ref(0);
const supplierOptions = ref();
const cityOptions = ref();
const orderTimes = ref();
const createdTimes = ref();

const loading = ref(false);

// 查询参数
const queryParams = reactive<MenuQuery>({
  pageNo: 1,
  pageSize: 10,
});
// 菜单表格数据
const TableData = ref([]);

// 选择表格的行菜单ID
const selectedMenuId = ref<number | undefined>();

function handleExport() {
  ExportApi.walletExport(queryParams).then((res) => {
    let data = res.data;
    ElMessage.success("导出完毕");
    if (!data) {
      return;
    }
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(new Blob([data], { type: "application/vnd.ms-excel" }), fileName + ".xls");
    } else {
      let url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.ms-excel" }));
      let link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute("download", "司机" + ".xlsx");
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  });
}

// 查询菜单
function handleQuery() {
  loading.value = true;
  DriverApi.getDriverRecords(queryParams)
    .then((data) => {
      TableData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleCreateTimeChange(time: any) {
  if (time) {
    queryParams.createTimeFrom = `${time[0]} 00:00:00`;
    queryParams.createTimeTo = `${time[1]} 23:59:59`;
  } else {
    queryParams.createTimeFrom = undefined;
    queryParams.createTimeTo = undefined;
  }
}
function handleOrderTimeChange(time: any) {
  if (time) {
    queryParams.orderTimeFrom = `${time[0]} 00:00:00`;
    queryParams.orderTimeTo = `${time[1]} 23:59:59`;
  } else {
    queryParams.orderTimeFrom = undefined;
    queryParams.orderTimeTo = undefined;
  }
}

// 重置查询
function handleResetQuery() {
  queryParams.orderTimeFrom = undefined;
  queryParams.orderTimeTo = undefined;
  queryParams.createTimeFrom = undefined;
  queryParams.createTimeTo = undefined;
  orderTimes.value = null;
  createdTimes.value = null;

  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  handleQuery();
}

// 行点击事件
function handleRowClick(row: MenuVO) {
  // 记录表格选择的菜单ID，新增子菜单作为父菜单ID
  selectedMenuId.value = row.id;
}

onMounted(() => {
  handleQuery();
  CommonApi.getSupplier({ pageNo: 1, pageSize: 9999, status: 1 }).then((data) => {
    if (data.code == 0) {
      supplierOptions.value = data.result;
    }
  });
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.red_rate {
  color: red;
}
.address_box {
  display: flex;
  flex-direction: column;
  align-items: left;
  justify-content: left;
  .item {
    display: flex;
    align-items: center;
    margin: 4px 0;
    .tag {
      width: 50px;
      height: 23px;
      text-align: center;
      color: orange;
      background: #fff8e6;
    }
    .tag2 {
      color: #3fd175;
      background: #e7faf0;
    }
  }
}
</style>
